package org.ichow.scenegame.core
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Dictionary;
	import org.ichow.scenegame.events.NotiEvent;
	import org.ichow.scenegame.notis.*;

	/**
	 * ...
	 * 提示控制
	 * @author M.ichow
	 */
	public class NotiFactory extends Sprite
	{
		private var _notis : Dictionary;
		private var _cuttentNoti : BaseNoti;

		public function NotiFactory()
		{
			_notis = new Dictionary();
		}

		/**
		 * 判断
		 * @param	v
		 * @return
		 */
		public function hasNoti( v : String ) : Boolean
		{
			return getNoti( v ) ? true : false;
		}

		/**
		 * 获取
		 * @param	v
		 * @return
		 */
		public function getNoti( v : String = null ) : BaseNoti
		{
			return _notis[ v ] ? _notis[ v ] : null;
		}

		/**
		 * 设置
		 * @param	v
		 * @param	d
		 * @param	isCreate
		 */
		public function setNoti( v : String , d : DisplayObject , isCreate : Boolean = false ) : void
		{
			if ( !_notis[ v ] || isCreate )
			{
				var noti : BaseNoti;
				switch ( v )
				{
					case NotiEvent.INIT:
						noti = new StartNoti();
						break;
					case NotiEvent.READY:
						noti = new GameInfoNoti();
						break;
					case NotiEvent.PLAY:
						noti = new OptionsNoti();
						noti.addEventListener( NotiEvent.SELECT_ITEM , onSelectItem );
						noti.addEventListener( NotiEvent.TOOLTIP , onTooltip );
						break;
					case NotiEvent.CONTINUE:
						noti = new ContinueNoti();
						break;
					case NotiEvent.END:
						noti = new EndNoti();
						break;
				}
				noti.addEventListener( NotiEvent.NOTI_CHANGE , onNotiChange );
				noti.bind( d , this );
				_notis[ v ] = noti;
			}
		}
		
		private function onTooltip(e:NotiEvent):void 
		{
			dispatchEvent( e );
		}

		private function onSelectItem( e : NotiEvent ) : void
		{
			dispatchEvent( e );
		}

		private function onNotiChange( e : NotiEvent ) : void
		{
			dispatchEvent( e );
		}

		/**
		 * 启用
		 * @param	v
		 */
		public function active( v : String ) : void
		{
			var noti : BaseNoti = getNoti( v );
			noti.active();
			_cuttentNoti = noti;
		}

		/**
		 * 更新
		 * @param	v
		 * @param	o
		 */
		public function update( v : String , o : Object = null ) : void
		{
			var noti : BaseNoti = getNoti( v );
			noti.update( o );
		}

		/**
		 * 重置
		 * @param	v
		 */
		public function reset( v : String = null ) : void
		{
			
			if ( v ) 
				getNoti( v ).reset();
			else
			{
				for each( var i : BaseNoti in _notis )
				{
					if ( i )
						i.reset();
				}
			}
		}

		/**
		 * 取消
		 * @param	v
		 */
		public function cancel( v : String ) : void
		{
			var noti : BaseNoti = getNoti( v );
			noti.cancel();
		}

		/**
		 * 獲取視圖名字
		 * @param	v
		 * @return
		 */
		public function getSkinName( v : String ) : String
		{
			var type : String;
			switch ( v )
			{
				case NotiEvent.INIT:
					type = "Start_btn";
					break;
				case NotiEvent.READY:
					type = "GameInfo_mc";
					break;
				case NotiEvent.PLAY:
					type = "Items_mc";
					break;
				case NotiEvent.CONTINUE:
					type = "Continue_mc";
					break;
				case NotiEvent.END:
					type = "GameEnd_mc";
					break;
				default:
					type = "Start_btn";
					break;
			}
			return type;
		}
		
		public function get ways():Array 
		{
			if ( getNoti( NotiEvent.PLAY ) is OptionsNoti )
			{
				return (getNoti( NotiEvent.PLAY ) as OptionsNoti).ways;
			}
			return null;
		}

	}

}